﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Navigation;
using DeepMixWorkflow;

namespace DeepmixRadioClient
{
    public partial class Settings
    {
        private bool _loaded;

        public Settings()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!_loaded)
            {
                string curAddress = StorageContainer.ServerAddress.Value;
                foreach (ListBoxItem item in lst.Items)
                {
                    if ((string) item.Tag == curAddress)
                    {
                        item.IsSelected = true;
                        item.Foreground = new SolidColorBrush(Colors.Green);
                    }
                }
                _loaded = true;
            }
        }

        private void sel_Changed(object sender, SelectionChangedEventArgs e)
        {
            if (!_loaded) return;
            foreach (ListBoxItem removedItem in e.RemovedItems)
            {
                removedItem.Foreground = new SolidColorBrush(Colors.White);
            }
            if (e.AddedItems.Count > 0)
            {
                var item = (ListBoxItem) e.AddedItems[0];
                StorageContainer.ServerAddress.Value = (string) item.Tag;

                MessageBox.Show(AppResources.AddressChanged);
                if (NavigationService.CanGoBack)
                {
                    try
                    {
                        NavigationService.GoBack();
                    }
                    catch (InvalidOperationException)
                    {
                        //случается, когда мы уже вышли из приложения
                    }
                }
            }
        }
    }
}